package com.dream.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.dream.able.ExperienceMapper;
import com.dream.datasource.DBContextHolder;
import com.dream.pojo.Experience;
import com.dream.pojo.ExperienceExample;
import com.dream.service.able.All_Service_able;
import com.dream.service.able.Limit_able;


/*
 * 一路走来跟新表业务类
 */
@Service("Experience_Seviceimpl")
public class Experience_Seviceimpl implements 
All_Service_able<Experience>,
Limit_able<Experience>{

	/*
	 * 一路走来表的DAO代理对象
	 */
	@Resource(name="experienceMapper")
	private ExperienceMapper experience_mapper;

	/*
	 * (添加一条记录,传入内容)
	 * @see com.dream.service.able.All_Service_able#insert(java.lang.String[])
	 */
	@Override
	public Experience insert(String... str) {
		//切换数据库，主库
		DBContextHolder.setDbType(DBContextHolder.master);
		//创建更新对象
		Experience experience = new Experience();
		//插入时间
		experience.setTime(new Date());
		//插入内容
		experience.setContext(str[0]);
		//插入状态
		experience.setState(1);
		//写入数据库
		return experience_mapper.insertSelective(experience)>0?experience:null;
	}

	/*
	 * (查询所有有效的一路走来的记录)
	 * @see com.dream.service.able.All_Service_able#list()
	 */
	@Override
	public List<Experience> list() {
		//切换数据库，从库
		DBContextHolder.setDbType(DBContextHolder.slave);
		//创建查询工具类
		ExperienceExample example = new ExperienceExample();
		//设置排序规则
		example.setOrderByClause("eid desc");
		//创建查询条件工具内部类
		ExperienceExample.Criteria criteria = example.createCriteria();
		//设置查询条件
		criteria.andStateEqualTo(1);
		//返回查询到的数据集
		return experience_mapper.selectByExample(example);
	}

	@Override
	public Experience getT(String... str) {
		return null;
	}

	/*
	 * (查询指定条数和位置的数据,传入开始的位置，和返回的数据数目)
	 * @see com.dream.service.able.Limit_able#list_Limit(java.lang.String[])
	 */
	@Override
	public List<Experience> list_Limit(String... str) {
		//切换数据库，从库
		DBContextHolder.setDbType(DBContextHolder.slave);
		//创建查询工具类
		ExperienceExample example = new ExperienceExample();
		//设置排序规则
		example.setOrderByClause("eid desc");
		//设置查询的开始位置
		example.setOffset(Integer.parseInt(str[0]));
		//设置返回的数目
		example.setLimit(Integer.parseInt(str[1]));
		//创建查询条件工具内部类
		ExperienceExample.Criteria criteria = example.createCriteria();
		//设置查询条件
		criteria.andStateEqualTo(1);
		//返回查询到的数据集
		return experience_mapper.selectByExample(example);
	}
}
